package com.clayecloud.module_main.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.module_main.R;
import com.clayecloud.module_main.databinding.ActivityScanBinding;
import com.gyf.immersionbar.ImmersionBar;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

/**
 * 先弃用，使用华为的scankit,以后可能用作自定义 扫描view
 */


@Route(path = "/main/scan/index")

public class ScanActivity extends AppCompatActivity {

    private ActivityScanBinding activityScanBinding;
    private CaptureManager capture;
    private DecoratedBarcodeView bv_barcode;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityScanBinding = DataBindingUtil.setContentView(this, R.layout.activity_scan);

        bv_barcode = activityScanBinding.bvBarcode;
        bv_barcode.setStatusText("请对准二维码");
        capture = new CaptureManager(this, bv_barcode);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();

        // 初始化头部导航栏
        ImmersionBar.with(this).init();


    }

    /**
     * 在删除此fragment时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }
    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return bv_barcode.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

}
